
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>MySQL 中DATETIME和TIMESTAMP类型的区别是什么？ - 学习卡片</title>
      <style>
        body { font-family: sans-serif; background-color: #f0f8ff; color: #333; display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
        .header h1 { font-size: 32px; }
        .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; width: 100%; max-width: 1200px; }
        .card-container { perspective: 1200px; cursor: pointer; height: 250px; }
        .card { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transition: transform 0.7s; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
        .card-container.flipped .card { transform: rotateY(180deg); }
        .card-face { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; flex-direction: column; box-sizing: border-box; border-radius: 16px; background-color: #fff; padding: 24px; }
        .card-back { background-color: #f0fff4; transform: rotateY(180deg); justify-content: space-between; }
        .card-category { font-size: 14px; color: #0052d9; margin-bottom: 8px; font-weight: 500; }
        .card-question { font-size: 20px; font-weight: 500; flex-grow: 1; display: flex; align-items: center; justify-content: center; text-align: center; }
        .card-answer-wrapper { flex-grow: 1; overflow-y: auto; }
        .card-answer { font-size: 15px; line-height: 1.7; }
        .card-footer { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 16px; margin-top: 16px; }
        .card-source { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 12px; margin-top: 12px; }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>MySQL 中DATETIME和TIMESTAMP类型的区别是什么？ - 学习卡片</h1>
      </div>
      <div class="grid-container">
        
    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">在MySQL中，`DATETIME`和`TIMESTAMP`类型在处理时区方面有何根本区别？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`DATETIME`类型不受时区影响，存储的时间是固定的，不会因时区变化而改变。而`TIMESTAMP`类型是时区相关的，它通常以UTC格式存储，在查询时会自动转换为当前系统的时区进行显示。</div>
          </div>
          <div class="card-source">来源: 3. 主要区别 - 时区处理</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">比较`DATETIME`和`TIMESTAMP`在存储空间占用和支持的时间范围上的差异。</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`DATETIME`占用8字节，支持的时间范围是从 '1000-01-01 00:00:00' 到 '9999-12-31 23:59:59'。`TIMESTAMP`占用4字节，支持的时间范围是从 '1970-01-01 00:00:01' UTC 到 '2038-01-19 03:14:07' UTC。</div>
          </div>
          <div class="card-source">来源: 3. 主要区别 - 范围和存储空间</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">哪种MySQL时间类型支持在记录更新时自动更新其值为当前时间？它是如何实现的？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`TIMESTAMP`类型支持自动更新时间。可以通过在表定义中使用`DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP`子句来实现。这样，每当记录被插入或更新时，`TIMESTAMP`列的值就会自动更新为当前时间。</div>
          </div>
          <div class="card-source">来源: 2. TIMESTAMP - 默认值</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">模式</div>
          <div class="card-question">根据文档建议，在什么场景下应该优先选择使用`DATETIME`类型？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">模式</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">当需要存储的时间超出了`TIMESTAMP`的范围（即早于1970年或晚于2038年），或者需要存储一个不受时区变化影响的、固定的时间信息时，应该选择`DATETIME`。</div>
          </div>
          <div class="card-source">来源: 4. 选择建议</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">模式</div>
          <div class="card-question">文档推荐在哪些应用场景下使用`TIMESTAMP`类型？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">模式</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">当需要存储的时间信息希望能够自动根据用户当前的时区进行调整显示，或者希望能够自动记录行的创建时间或最后更新时间时，应该选择`TIMESTAMP`。</div>
          </div>
          <div class="card-source">来源: 4. 选择建议</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">在MySQL中，如果没有显式为`DATETIME`列指定默认值，它的默认值是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">如果没有显式为`DATETIME`列指定默认值，MySQL会将其默认值设置为`NULL`。</div>
          </div>
          <div class="card-source">来源: 1. DATETIME - 默认值</div>
        </div>
      </div>
    </div>

      </div>
    </body>
    </html>
